package com.kejy.web;

import com.kejy.entity.AuctionUser;
import com.kejy.mapper.AuctionUserMapper;
import com.kejy.util.MyBatisUtil;
import com.kejy.util.RequestUtil;
import my.framework.anno.RequestMapping;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;



public class AuctionUserServlet {

    @RequestMapping(value = "/register")
    public void register(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        SqlSession sqlSession = null;

        try  {
            sqlSession = MyBatisUtil.createSqlSession();
            AuctionUserMapper mapper = sqlSession.getMapper(AuctionUserMapper.class);

            // 获取参数
            String userName = request.getParameter("userName");
            String userPassword = request.getParameter("userPassword");
            String userCardNo = request.getParameter("userCardNo");
            String userTel = request.getParameter("userTel");
            String userAddress = request.getParameter("userAddress");
            String userPostNumber = request.getParameter("userPostNumber");
            // 验证邮政编码格式和长度
            if (userPostNumber == null || !userPostNumber.matches("\\d{6}")) {
                response.getWriter().write("邮政编码必须为6位数字");
                return;
            }

            AuctionUser auctionUser = new AuctionUser(userName,  userPassword, userCardNo, userTel, userAddress, userPostNumber);
            mapper.addUser(auctionUser);
            sqlSession.commit();
            //  跳转到登录页面，使用绝对路径
            response.sendRedirect(request.getContextPath() + "/login.jsp");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @RequestMapping(value = "/login")
    public void login(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        SqlSession sqlSession = null;
        try  {
            sqlSession = MyBatisUtil.createSqlSession();
            AuctionUserMapper mapper = sqlSession.getMapper(AuctionUserMapper.class);
            String userName = request.getParameter("userName");
            String userPassword = request.getParameter("userPassword");
            boolean userIs = RequestUtil.parseInt(request, "userIs", 0) == 1;
            AuctionUser user = mapper.login(new AuctionUser(userName, userPassword));
            if (user != null && user.isUserIsAdmin() == userIs) {
                HttpSession session = request.getSession();
                session.setAttribute("user", user);
                if (user.isUserIsAdmin() == true){
                    // 登录成功
                    //  跳转到拍卖管理页面页面，使用绝对路径
                    response.sendRedirect(request.getContextPath() + "/do/pageList");
                } else {
                    // 登录成功
                    //  跳转到拍卖页面页面，使用绝对路径
                    response.sendRedirect(request.getContextPath() + "/do/pageList");
                }
            }else {
                response.getWriter().write("用户名或密码错误");
                return;
            }
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }
}
